const db = require('../db/index')

exports.addNotice = (req, res) => {
  const noticeinfo  = {
    ...req.body,
    noticer: req.user.user_name
  }
  const sql = `insert into ag_notice set ?`
  db.query(sql, noticeinfo, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('关注失败!')
    res.cc('关注成功！', 0)
  })
}

exports.noticeList = (req, res) => {
  const sql = `select * from ag_notice where noticer=?`
  db.query(sql, req.user.user_name, (err, results) => {
    if(err) return res.cc(err)
    if(results.length === 0) return res.cc('获取关注列表失败!')
    res.send({
      status: 0,
      message: '获取关注列表成功!',
      data: results
    })
  })
}

exports.deleteNotice = (req, res) => {
  const sql = `delete from ag_notice where id=?`
  db.query(sql, req.body.id, (err, results) => {
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('取消关注失败!')
    res.cc('取消关注成功!', 0)
  })
}